home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / Weather / Weather.app / MyMenuCell.m < prev    next >
Text File  |  1992-02-20  |  3KB  |  117 lines

  1. /*
  2.  * This menu cell is a cell in the weather menu browser.
  3.  * Knows how to transmit codes and fetch bits of text,
  4.  * which is somewhat special-cased for this service.
  5.  * M. J. Hawley
  6.  * mike@media-lab.mit.edu
  7.  * Copyright (c) November 1991, MIT Media Laboratory.
  8.  */
  9. #import "MyMenuCell.h"
  10. #import "Process.h"
  11. #import <string.h>
  12. #import <ctype.h>
  13.  
  14. @implementation  MyMenuCell
  15.  
  16. char *getLabel(), *getSend(), *getGet(), *curMenuFirst(), *curMenuLast();
  17. extern char *curMenu, *path();
  18. extern int Verbose;
  19.  
  20. void
  21. Say(a,b,c,d,e) char *a, *b, *c, *d, *e; {
  22.     char s[1024];
  23.     sprintf(s,a,b,c,d,e);
  24.     setText(s);
  25. }
  26.  
  27. - click:sender {
  28.     if (*send == '!'){ // run a command
  29.         char t[1024], *p = send+1, *s;
  30.         *t = '\0'; sscanf(send+1,"%s",t);
  31.         if (*t && (s = path(t))){
  32.             p += strlen(t);
  33.             System("%s %s &",s,p);
  34.         } else
  35.             System("%s &",send+1);
  36.         setText("\nThat request is running quietly in the background.");
  37.         return self;
  38.     } else
  39.     if (*send == '|'){ // read from a pipe
  40.         char t[1024], *p = send+1, *s, ff[1024];
  41.         FILE *f;
  42.         *t = '\0'; sscanf(p,"%s",t);
  43.         if (*t && (s = path(t))){
  44.             p += strlen(t);
  45.             sprintf(ff,"%s %s",s,p);
  46.         } else
  47.             sprintf(ff,"%s",p);
  48.         if (f=popen(ff,"r")){
  49.             char buf[120000], *b = buf;
  50.             *b = '\0';
  51.             Say("\ntrying: %s...", ff);
  52.             while (fgets(b,sizeof b,f))
  53.                 b += strlen(b);
  54.             pclose(f);
  55.             setText(buf);
  56.         } else
  57.             setText("\nCouldn't fetch that data.");
  58.         return self;
  59.     } else
  60.     if (*send == '<'){ // open a file
  61.         char *p = send+1;
  62.         if (*p == '<'){ // ... via workspace
  63.             openFile(path(p+1));
  64.         } else
  65.             setFile(p); // ... locally
  66.         return self;
  67.     } else if (*send)
  68.         ensurelogin(), Put("%s",send);
  69.     if ([self isLeaf]) setFetchText(label);
  70.     if (strcmp("text",get)==0){
  71.         char buf[80000];
  72.         getReport(buf);
  73.         if (*buf) setText(buf);
  74.     }
  75.     return self;
  76. }
  77.  
  78. - click2:sender {
  79.     return self;
  80. }
  81.  
  82. - setStringValueNoCopy:(char *)s {
  83.     label = getLabel(s);
  84.     send = getSend(s);
  85.     get = getGet(s);
  86.     [super setStringValue:label];
  87.     [self setLeaf:state(get)?NO:YES];
  88.     //[self setAction:@selector(click:)];
  89.     [self setTarget:self];
  90.     return self;
  91. }
  92.  
  93. - setStringValue: (char *)s {
  94.     return [self setStringValueNoCopy:s];
  95. }
  96.  
  97. - (char *)stringValue {
  98.     return label;
  99. }
  100.  
  101. - (char *)get {
  102.     return get;
  103. }
  104.  
  105. - send {
  106.     Put("%s",send);
  107.     return self;
  108. }
  109.  
  110. - mouseDown:(NXEvent *)e {
  111.     [super mouseDown:e];
  112.         if (e->data.mouse.click == 1){
  113.                 [self click:self];
  114.         }
  115. }
  116.  
  117. @end